#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define ep emplace
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;
using tl = tuple < int, int, int >;

ll read() {
	ll x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

const int N = 1e6 + 10;
const int M = 1e3 + 10;

int p[N], n;
ll K;

deque < int > pot1, pot2;

int getmn() { int x; if(pot1.size()) { x = pot1.front(); pot1.pop_front(); } else x = pot2.front(), pot2.pop_front(); return x; }
bool getmx() { if(pot2.size() == 0) return false; pot2.pop_back(); return true; }
int getfr() { int x = pot2.front(); pot2.pop_front(); return x; }

bool check(int lim) {
	ll lef = K;
	int md = (n + 2) >> 1;
	pot1.clear(); pot2.clear();
	rep(i, 1, lim - 1) if(p[i] < p[lim]) pot1.pb(i); else pot2.pb(i);
	rep(i, lim, n * n) pot2.pb(i);
	rep(i, 1, n) {
		rep(j, 1, md - 1) { getmn(); }
		if(!pot2.size()) return 0;
		lef -= p[getfr()];
		if(lef < 0) return 0;
		rep(j, md + 1, n) if(!getmx()) return 0;
	} return 1;
}

void solve() {
	n = in; K = in; rep(i, 1, n * n) p[i] = in;
	sort(p + 1, p + n * n + 1);
	int l = 1, r = n * n, ans = -1;
	while(l <= r) {
		int mid = (l + r) >> 1;
		if(check(mid)) l = mid + 1, ans = p[mid];
		else r = mid - 1;
	}
	printf("%d\n", ans);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	int T = in;
	while(T--) solve();
	return 0;
}
